
const Service = require('./base_service')

class AccountSerServer extends Service {
  constructor (...props) {
    super(...props)
    this.tableName = 'sys_account_service_grant'
  }
  //查询列表
  index (id) {
    let queryString = `
      select a.id
      , a.extend
      , a.account_id AS "accountId"
      , a.service_id AS "serviceId"
      , b.account_name AS "accountName"
      , b.account_alias AS "accountAlias"
      , c.name AS "serviceName"
      , c.alias AS "serviceAlias"
      , c.comment AS "serviceComment"
      FROM
      (select id,account_id,service_id,extend from sys_account_service_grant) a
      LEFT JOIN
      (select id,account_name,account_alias from sys_account) b on a.account_id=b.id
      LEFT JOIN
      (select id,name,alias,comment from sys_service) c on a.service_id=c.id
      WHERE a.account_id=${id}
      ORDER BY a.id
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then((client) => {
          client.query(queryString, (err, res) => {
            client.release()
            if (err) {
              return reject(err)
            }
            resolve(res)
          })
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  //新建
  create (props) {
    let { accountId, serviceId, extend } = props
    let params = [accountId, serviceId, extend]
    let queryString = `
      INSERT INTO 
      ${this.tableName}(account_id, service_id, extend)
      VALUES($1, $2, $3)
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            queryString
            , params
            , (err, res) => {
              client.release()
              if (err) return reject(err)
              resolve(res)
            }
          )
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  update (props) {
    console.log('in service')
    let { id, accountId, serviceId, extend } = props
    let params = [id, accountId, serviceId, extend]
    console.log(1)
    console.log(typeof extend)
    let queryString = `
      UPDATE ${this.tableName} SET 
      account_id = $2, 
      service_id = $3,
      extend = $4
      WHERE id = $1
    `
    console.log(extend,typeof extend)
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            queryString
            , params
            , (err, res) => {
              client.release()
              if (err) return reject(err)
              resolve(res)
            }
          )
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
  delete (id) {
    let queryString = `
      DELETE FROM ${this.tableName} WHERE id = $1
    `
    return new Promise((resolve, reject) => {
      this.postgreServer()
        .connect()
        .then(client => {
          client.query(
            queryString
            , [id]
            , (err, res) => {
              client.release()
              if (err) return reject(err)
              resolve(res) 
            }
          )
        })
        .catch(e => {
          this.ctx.logger.error(e)
        })
    })
  }
}

module.exports = AccountSerServer
